{% extends 'base.html' %}
{% load staticfiles %}
{% load platfom_tags %}


{% block base_title %}
    运维事件
{% endblock base_title %}


{% block other_css %}
    <!-- 弹框详情css -->
    <style>
        .popover-title {
            color: #1c2b36;
            font-weight: bolder;
        }

        .popover {
            background-color: #1c2b36;
            color: white;
        }
    </style>
{% endblock other_css %}

{% block base_content %}
    <!-- 右边主体 -->
    <div class="app-content">
        <div class="app-content-body fade-in-up">
            <div class="padder" style="padding-top: 10px;">

                <div class="col-md-12" style="padding-left: 0; padding-right: 0;">
                    <div class="panel panel-default" id="body-content">
                        <div class="panel-heading">
                            <div style="display: block; border-left: 5px solid #1c2b36; padding-left: 10px; font-size: 20px; font-weight: bolder; line-height: 25px;color: #1c2b36;">
                                运维事件{% ifnotequal keyword '' %}&nbsp;&nbsp;："{{ keyword }}" 关键字搜索结果{% endifnotequal %}
                            </div>
                        </div>

                        <div class="panel-body" id="table_content">

                            <div class="selectBox">
                                <a class="btn btn-sm btn-default" data-toggle="modal"
                                   data-target="#AddOpEventModal">
                                    <i class="fa fa-plus"></i> 添加事件
                                </a>

                                <!-- 功能 -->
                                <form class="navbar-form pull-right " role="search" style="margin-top: 0;">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <input type="text" name="keyword"
                                                   class="form-control input-sm bg-light no-border rounded "
                                                   placeholder="关键词查找记录" style="width: 150px;">
                                            <span class="input-group-btn">
                                            <button type="submit" class="btn btn-sm bg-auto rounded">
                                                <i class="fa fa-search"></i>
                                            </button>
                                        </span>
                                        </div>
                                    </div>
                                </form>
                                <span style="font-size: 14px; padding-top: 5px;" class="pull-right">共 <b
                                        style="color: orangered">{{ record_nums }}</b> 条</span>
                            </div>

                            <!-- 列表 -->
                            <div class="row">
                                <div class="col-sm-12">
                                    <table class="table table-striped table-bordered">
                                        <!-- 标题行 -->
                                        <thead>
                                        <tr role="row">
                                            <th class="text-center" width="60">ID</th>
                                            <th class="text-center">事件名称</th>
                                            <th class="text-center">开始时间</th>
                                            <th class="text-center">结束时间</th>
                                            <th class="text-center">处理人</th>
                                            <th class="text-center">备注</th>
                                            <th class="text-center" width="200">操作</th>
                                        </tr>
                                        </thead>
                                        <tbody>

                                        {% for each_record in records.object_list %}
                                            <tr class="gradeX odd" role="row">
                                                <td class="text-center"
                                                    style="vertical-align:middle;">{{ forloop.counter }}</td>
                                                <td class="text-center"
                                                    style="vertical-align:middle;">{{ each_record.name }}</td>
                                                <td class="text-center"
                                                    style="vertical-align:middle;">{{ each_record.start_time }}
                                                </td>
                                                <td class="text-center"
                                                    style="vertical-align:middle;">{{ each_record.stop_time }}</td>
                                                <td class="text-center"
                                                    style="vertical-align:middle;">
                                                    {% for each in each_record.op_user.all %}
                                                        {% if forloop.counter == 1 %}{{ each.chinese_name }}{% else %},
                                                            {{ each.chinese_name }}{% endif %}
                                                    {% endfor %}
                                                </td>
                                                <td class="text-center"
                                                    style="vertical-align:middle;">{% if each_record.desc %}
                                                    <button style="color: #104E8B; border: none; background:none"
                                                            title="备注"
                                                            data-container="body" data-toggle="popover"
                                                            data-trigger="hover"
                                                            data-placement="top"
                                                            data-content="{{ each_record.desc }}">
                                                        {{ each_record.desc|slice:"5" }}
                                                    </button>{% else %}暂无{% endif %}</td>

                                                <td class="text-center" style="vertical-align:middle;">

                                                    {% if request.user.role > 2 %}
                                                        <a class="btn btn-xs" data-toggle="modal"
                                                           data-target="#EditOpEventModal{{ each_record.id }}"
                                                           style="background-color: #009688;color: white;">
                                                            编辑
                                                        </a>
                                                        <a class="btn btn-xs" data-toggle="modal"
                                                           data-target="#DeleteOpEventModal{{ each_record.id }}"
                                                           style="background-color: #f05050;color: white;">
                                                            删除
                                                        </a>
                                                    {% else %}
                                                        {% if request.user == each_record.deploy_user %}
                                                            <a class="btn btn-xs" data-toggle="modal"
                                                               data-target="#EditOpEventModal{{ each_record.id }}"
                                                               style="background-color: #009688;color: white;">
                                                                编辑
                                                            </a>
                                                            <a class="btn btn-xs"
                                                               data-target="#DeleteOpEventModal{{ each_record.id }}"
                                                               style="background-color: #f05050;color: white;">
                                                                删除
                                                            </a>
                                                        {% else %}
                                                            <span class="btn btn-xs" data-toggle="modal"
                                                                  style="background-color: gray;color: white;">
                                                            编辑
                                                        </span>
                                                            <span class="btn btn-xs"
                                                                  style="background-color: gray;color: white;">
                                                            删除
                                                        </span>
                                                        {% endif %}
                                                    {% endif %}

                                                </td>
                                            </tr>
                                        {% endfor %}

                                        </tbody>
                                    </table>
                                </div>
                            </div>

                        </div>

                        <!-- 页码 -->
                        <div class="panel-footer" style="padding-top: 0; padding-bottom: 0;">
                            <div class="text-center">
                                <ul class="pagination" style="margin-top: 10px; margin-bottom: 10px;">

                                    <!-- 上一页 -->
                                    {% if records.has_previous %}
                                        <li><a href="?{{ records.previous_page_number.querystring }}">上一页<span
                                                class="sr-only"></span></a></li>
                                        <li><a href="?{{ records.previous_page_number.querystring }}">&laquo;<span
                                                class="sr-only"></span></a></li>
                                    {% endif %}

                                    <!-- 页码 -->
                                    {% for page in records.pages %}
                                        {% if page %}
                                            <!-- 当前页 -->
                                            {% ifequal page records.number %}
                                                <li class="active"><a href="?page={{ page }}">{{ page }}<span
                                                        class="sr-only">(current)</span></a></li>
                                                <!-- 其它页 -->
                                            {% else %}
                                                <li><a href="?page={{ page }}">{{ page }}<span
                                                        class="sr-only"></span></a></li>
                                            {% endifequal %}
                                            <!-- 省略页 -->
                                        {% else %}
                                            <li><a href="">...<span class="sr-only"></span></a></li>
                                        {% endif %}
                                    {% endfor %}

                                    <!-- 下一页 -->
                                    {% if records.has_next %}
                                        <li><a href="?{{ records.next_page_number.querystring }}">&raquo;<span
                                                class="sr-only"></span></a></li>
                                        <li><a href="?{{ records.next_page_number.querystring }}">下一页<span
                                                class="sr-only"></span></a></li>
                                    {% endif %}
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

{% endblock base_content %}

{% block base_footer_html %}
    <div class="modal inmodal" id="AddOpEventModal" tabindex="-1" role="dialog"
         aria-hidden="true"
         data-backdrop="static">
        <div class="modal-dialog" style="width: 450px;">
            <div class="modal-content" style="margin-top: 100px;">

                <div class="modal-header"
                     style="background-color: #1c2b36; color: white;padding-top: 10px; padding-bottom: 10px;">
                    <button type="button" class="close" data-dismiss="modal">
                        <i class="icon-close" style="font-size: 18px;"></i>
                    </button>
                    <h4 class="modal-title">添加运维事件</h4>
                </div>

                <form method="post" id="id_AddOpEventForm">
                    <div class="modal-body" style="height: 250px;">

                        <div class="form-group" style="vertical-align: middle;line-height: 34px;padding-bottom: 30px;">
                            <div class="col-md-3">
                                <span style="font-size: 14px;">事件名称：</span>
                            </div>
                            <div class="col-md-9" style="padding: 0;">
                                <input type="text" class="form-control" placeholder="事件名称" maxlength="100" name="name"
                                       required="">
                            </div>
                        </div>

                        <div class="form-group" style="vertical-align: middle;line-height: 34px;padding-bottom: 30px;">
                            <div class="col-md-3">
                                <span style="font-size: 14px;">开始时间：</span>
                            </div>
                            <div class="col-md-9" style="padding: 0;">
                                <div>
                                    <div class="input-group date" id='datetimepicker_add1'>
                                        <span class="input-group-addon">
                                            <i class="fa fa-calendar"></i>
                                        </span>
                                        <input name="start_time" type="text" class="form-control" value="">
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="form-group" style="vertical-align: middle;line-height: 34px;padding-bottom: 30px;">
                            <div class="col-md-3">
                                <span style="font-size: 14px;">结束时间：</span>
                            </div>
                            <div class="col-md-9" style="padding: 0;">
                                <div>
                                    <div class="input-group date" id='datetimepicker_add2'>
                                        <span class="input-group-addon">
                                            <i class="fa fa-calendar"></i>
                                        </span>
                                        <input name="stop_time" type="text" class="form-control" value="">
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="form-group" style="vertical-align: middle;line-height: 34px;padding-bottom: 30px;">
                            <div class="col-md-3">
                                <span style="font-size: 14px;">处理人：</span>
                            </div>
                            <div class="col-md-9" style="padding: 0;">
                                <select name="op_user" class="selectpicker form-control" multiple=""
                                        data-live-search="true"
                                        style="display: none;">
                                    {% for user in users %}
                                        <option value="{{ user.id }}">{{ user.chinese_name }}</option>
                                    {% endfor %}
                                </select>
                            </div>
                        </div>

                        <div class="form-group" style="vertical-align: middle;line-height: 34px;padding-bottom: 30px;">
                            <div class="col-md-3">
                                <span style="font-size: 14px;">备注：</span>
                            </div>
                            <div class="col-md-9" style="padding: 0;">
                                <input type="text" class="form-control" placeholder="非必须" maxlength="100" name="desc"
                                       required="">
                            </div>
                        </div>

                    </div>
                </form>


                <div class="modal-footer" style="padding-right: 15px;padding-top: 10px; padding-bottom: 10px;">
                    <a class="btn btn-sm btn-default" data-dismiss="modal">取消</a>
                    <a class="btn btn-sm btn-default" id="id_AddOpEventBtn">添加</a>
                </div>

            </div>
        </div>
    </div>

    <script>
        $(function () {
            // 提交表单
            $('#id_AddOpEventBtn').on('click', function () {
                $.ajax({
                    cache: false,
                    type: "POST",
                    url: "{% url 'online_management:add_opevent' %}",
                    data: $('#id_AddOpEventForm').serialize(),
                    async: true,
                    beforeSend: function (xhr, settings) {
                        xhr.setRequestHeader("X-CSRFToken", "{{ csrf_token }}");
                    },
                    success: function (data) {
                        if (data.status == 'success') {
                            window.location.reload();
                        } else if (data.status == 'failed') {
                            window.alert(data.msg);
                        }
                    }
                });
            });
        })
    </script>


    {% for each_record in records.object_list %}
        <div class="modal inmodal" id="EditOpEventModal{{ each_record.id }}" tabindex="-1" role="dialog"
             aria-hidden="true"
             data-backdrop="static">
            <div class="modal-dialog" style="width: 450px;">
                <div class="modal-content" style="margin-top: 100px;">

                    <div class="modal-header"
                         style="background-color: #1c2b36; color: white;padding-top: 10px; padding-bottom: 10px;">
                        <button type="button" class="close" data-dismiss="modal">
                            <i class="icon-close" style="font-size: 18px;"></i>
                        </button>
                        <h4 class="modal-title">修改记录</h4>
                    </div>

                    <form method="post" id="id_EditOpEventForm{{ each_record.id }}">
                        <div class="modal-body" style="height: 250px;">

                            <input type="hidden" name="record_id" value="{{ each_record.id }}">

                            <div class="form-group"
                                 style="vertical-align: middle;line-height: 34px;padding-bottom: 30px;">
                                <div class="col-md-3">
                                    <span style="font-size: 14px;">事件名称：</span>
                                </div>
                                <div class="col-md-9" style="padding: 0;">
                                    <input type="text" class="form-control" placeholder="事件名称" maxlength="100"
                                           name="name"
                                           required="" value="{{ each_record.name }}">
                                </div>
                            </div>

                            <div class="form-group"
                                 style="vertical-align: middle;line-height: 34px;padding-bottom: 30px;">
                                <div class="col-md-3">
                                    <span style="font-size: 14px;">开始时间：</span>
                                </div>
                                <div class="col-md-9" style="padding: 0;">
                                    <div>
                                        <div class="input-group date" id='datetimepicker_edit{{ each_record.id }}_1'>
                                        <span class="input-group-addon">
                                            <i class="fa fa-calendar"></i>
                                        </span>
                                            <input name="start_time" type="text" class="form-control"
                                                   value="{{ each_record.start_time|date:"Y-m-d H:i" }}">
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="form-group"
                                 style="vertical-align: middle;line-height: 34px;padding-bottom: 30px;">
                                <div class="col-md-3">
                                    <span style="font-size: 14px;">结束时间：</span>
                                </div>
                                <div class="col-md-9" style="padding: 0;">
                                    <div>
                                        <div class="input-group date" id='datetimepicker_edit{{ each_record.id }}_2'>
                                        <span class="input-group-addon">
                                            <i class="fa fa-calendar"></i>
                                        </span>
                                            <input name="stop_time" type="text" class="form-control" value="
                                                    {% if each_record.stop_time %}{{ each_record.stop_time|date:"Y-m-d H:i" }}{% endif %}">
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="form-group"
                                 style="vertical-align: middle;line-height: 34px;padding-bottom: 30px;">
                                <div class="col-md-3">
                                    <span style="font-size: 14px;">处理人：</span>
                                </div>

                                <div class="col-md-9" style="padding: 0;">
                                    <select name="op_user" class="selectpicker form-control" multiple=""
                                            data-live-search="true"
                                            style="display: none;">
                                        {% for user in users %}
                                            <option value="{{ user.id }}"
                                                    {% if user in each_record.op_user.all %}selected{% endif %}>{{ user.chinese_name }}</option>
                                        {% endfor %}
                                    </select>
                                </div>
                            </div>

                            <div class="form-group"
                                 style="vertical-align: middle;line-height: 34px;padding-bottom: 30px;">
                                <div class="col-md-3">
                                    <span style="font-size: 14px;">备注：</span>
                                </div>
                                <div class="col-md-9" style="padding: 0;">
                                    <input type="text" class="form-control" placeholder="非必须" maxlength="100"
                                           name="desc"
                                           required=""
                                           value="{% if each_record.desc %}{{ each_record.desc }}{% endif %}">
                                </div>
                            </div>

                        </div>
                    </form>


                    <div class="modal-footer" style="padding-right: 15px;padding-top: 10px; padding-bottom: 10px;">
                        <a class="btn btn-sm btn-default" data-dismiss="modal">取消</a>
                        <a class="btn btn-sm btn-default" id="id_EditOpEventBtn{{ each_record.id }}">保存</a>
                    </div>

                </div>
            </div>
        </div>

        <script>
            $(function () {
                // 提交表单
                $('#id_EditOpEventBtn{{ each_record.id }}').on('click', function () {
                    $.ajax({
                        cache: false,
                        type: "POST",
                        url: "{% url 'online_management:edit_opevent' %}",
                        data: $('#id_EditOpEventForm{{ each_record.id }}').serialize(),
                        async: true,
                        beforeSend: function (xhr, settings) {
                            xhr.setRequestHeader("X-CSRFToken", "{{ csrf_token }}");
                        },
                        success: function (data) {
                            if (data.status == 'success') {
                                window.location.reload();
                            } else if (data.status == 'failed') {
                                window.alert(data.msg);
                            }
                        }
                    });
                });
            })
        </script>

        <!-- 删除 -->
        <div class="modal inmodal" id="DeleteOpEventModal{{ each_record.id }}" tabindex="-1" role="dialog"
             aria-hidden="true"
             data-backdrop="static">
            <div class="modal-dialog modal-sm" style="padding-top: 10%;">
                <div class="modal-content">

                    <div class="modal-header"
                         style="background-color: #1c2b36; color: white;padding-top: 10px; padding-bottom: 10px;">
                        <button type="button" class="close" data-dismiss="modal">
                            <i class="icon-close" style="font-size: 18px;"></i>
                        </button>
                        <h4 class="modal-title">警告！</h4>
                    </div>

                    <form method="post" id="id_DeleteOpEventForm{{ each_record.id }}">
                        <input type="hidden" name="record_id" value="{{ each_record.id }}">
                        <div class="modal-body">
                            <span style="font-size: 16px;color: black;">是否确定删除该记录？</span>
                        </div>
                    </form>

                    <div class="modal-footer" style="padding: 10px;padding-top: 10px; padding-bottom: 10px;">
                        <a class="btn btn-default" data-dismiss="modal" style="padding: 2px 10px;">取消</a>
                        <a class="btn btn-default" id="id_DeleteOpEventBtn{{ each_record.id }}"
                           style="padding: 2px 10px;">确定</a>
                    </div>

                </div>
            </div>
        </div>

        <!-- 删除 -->
        <script>
            $(function () {
                // 提交表单
                $('#id_DeleteOpEventBtn{{ each_record.id }}').on('click', function () {
                    $.ajax({
                        cache: false,
                        type: "POST",
                        url: "{% url 'online_management:del_opevent' %}",
                        data: $('#id_DeleteOpEventForm{{ each_record.id }}').serialize(),
                        async: true,
                        beforeSend: function (xhr, settings) {
                            xhr.setRequestHeader("X-CSRFToken", "{{ csrf_token }}");
                        },
                        success: function (data) {
                            if (data.status == 'success') {
                                window.location.reload();
                            } else if (data.status == 'failed') {
                                window.alert(data.msg);
                            }
                        }
                    });
                });
            })
        </script>
    {% endfor %}
{% endblock base_footer_html %}


{% block base_footer_js %}
    <link rel="stylesheet" href="{% static 'plugins/bootstrap_date/bootstrap-select.css' %}" type="text/css">
    <script src="{% static 'plugins/bootstrap_date/bootstrap-select.js' %}"></script>
    <script src="{% static 'plugins/bootstrap_date/moment-with-locales.min.js' %}"></script>
    <link href="{% static 'plugins/bootstrap_date/bootstrap-datetimepicker.min.css' %}" rel="stylesheet">
    <script src="{% static 'plugins/bootstrap_date/bootstrap-datetimepicker.min.js' %}"></script>

    <!-- 添加记录时间选择器 -->
    <script>
        $('#datetimepicker_add1').datetimepicker({
            format: 'YYYY-MM-DD HH:mm',
            locale: moment.locale('zh-cn')
        });
    </script>

    <script>
        $('#datetimepicker_add2').datetimepicker({
            format: 'YYYY-MM-DD HH:mm',
            locale: moment.locale('zh-cn')
        });
    </script>

    <!-- 下拉选择框 -->
    <script type="text/javascript">
        $(window).on('load', function () {
            $('.selectpicker').selectpicker({
                'selectedText': 'cat'
            });
        })
    </script>

    <!-- 记录详情 -->
    <script>
        $(function () {
            $("[data-toggle='popover']").popover();
        });
    </script>

    {% for each_record in records.object_list %}
        <!-- 搜索记录时间选择器 -->
        <script>
            $('#datetimepicker_edit{{ each_record.id }}_1').datetimepicker({
                format: 'YYYY-MM-DD HH:mm',
                locale: moment.locale('zh-cn')
            });
        </script>

        <script>
            $('#datetimepicker_edit{{ each_record.id }}_2').datetimepicker({
                format: 'YYYY-MM-DD HH:mm',
                locale: moment.locale('zh-cn')
            });
        </script>
    {% endfor %}

{% endblock base_footer_js %}